package com.wenbo.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.wenbo.dao.IRelationshipDao;
import com.wenbo.dao.IUserInfoDao;
import com.wenbo.model.Relationship;
import com.wenbo.model.UserInfo;

@Component(value="userInfoService")
public class UserInfoServiceImpl implements IUserInfoService {
	
	@Autowired
	private IUserInfoDao userInfoDao;
	
	@Autowired
	private IRelationshipDao relationshipDao;

	@Override
	public UserInfo findUserInfoById(Integer id)
	{
		return this.userInfoDao.load(id);
	}

	@Override
	public void save(UserInfo userInfo)
	{
		this.userInfoDao.save(userInfo);
	}

	@Override
	public void update(UserInfo userInfo) {
		this.userInfoDao.update(userInfo);
	}

	public void setUserInfoDao(IUserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}

	@Override
	public UserInfo findUserInfoByName(String name) 
	{
		return this.userInfoDao.findUserInfoByName(name);
	}

	@Override
	public void changeUserState(UserInfo userInfo) {
		this.userInfoDao.update(userInfo);
		
	}

	@Override
	public List<UserInfo> findFriends(UserInfo userInfo){
		
		return this.userInfoDao.findUserFrinds(userInfo);
	}

	@Override
	public List<UserInfo> findUserinfos(UserInfo userInfo,int pageSize,int pageIndex) {
		
		return this.userInfoDao.findByPage(findByCriteria(userInfo), pageSize, pageIndex);
	}
	
	
	private DetachedCriteria findByCriteria(UserInfo userInfo)
	{
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(UserInfo.class);
		if(StringUtils.hasText(userInfo.getUserName()))
		{
			detachedCriteria.add(Restrictions.not(Restrictions.eq("userName",userInfo.getUserName())));
		}
		detachedCriteria.add(Restrictions.eq("state","0"));
		return detachedCriteria;
	}

	@Override
	public boolean addUsersToFriend(String username, String username1) 
	{
		boolean flag = false;
		UserInfo user1 = findUserInfoByName(username);
		UserInfo user2 = findUserInfoByName(username1);
		Relationship relationship = new Relationship();
		relationship.setUserId(user1.getId());
		relationship.setFriendId(user2.getId());
		this.relationshipDao.save(relationship);
		relationship = new Relationship();
		relationship.setUserId(user2.getId());
		relationship.setFriendId(user1.getId());
		this.relationshipDao.save(relationship);
		flag = true;
		return flag;
	}

	@Override
	public void saveUserDownlineMessage(String msg, String username) 
	{
		UserInfo userInfo = this.findUserInfoByName(username);
		userInfo.setUserDownlineMessage(userInfo.getUserDownlineMessage()+msg);
		this.update(userInfo);
	}

	@Override
	public void delUserDownlineMessage(String username) 
	{
		UserInfo userInfo = this.findUserInfoByName(username);
		userInfo.setUserDownlineMessage("");
		this.update(userInfo);
	}

}
